<?php

//Data loading
//if (GetParam("m","")=="AUDIT")
  //  $title='BALANCE SHEET (AUDIT)';
//else
    $title='BALANCE SHEET';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. AMPEL JAYA';
$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new COAPDFReport("P","mm","Letter");
$pdf->DBConnection($DBConnection->dbc);
$pdf->AddPage();
$pdf->GenerateReport();
$pdf->Output();

class COAPDFReport extends PDF {
  /*function COAPDFReport($Page,$Size,$PageMode) {
    parent::PDF($Page,$Size,$PageMode);

    $this->ColSize = array(55,23,23,55,23,23);
    $this->TableName = "tblcoa";
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
    $this->Period(GetParam("y",""), GetParam("m",""));
	
    //$a = GetParam("y","");
    //$b = GetParam("m","");
    //echo "$a, $b";
    //exit;
  }*/

  function Header() {
  	$this->ColSize = array(55,23,23,55,23,23);
    $this->AddFont('arialn','','arialn.php');
    $this->SetFont('arialn','',10);
    $this->SetLeftMargin(5);
    $this->Period(GetParam("y",""), GetParam("m",""));
      $title = "BALANCE SHEET";
      $page   = 'HAL';

      //if (GetParam("m","")=="AUDIT")
        //$title='BALANCE SHEET (AUDIT)';
      //else
        //$title='BALANCE SHEET';

      $title .= (GetParam("m","")=="13")?' (AUDIT)':"";
      $perush = (GetParam("office",""))?GetCompany (GetParam("office","")):'PT. MASAJI PRAYASA CARGO';

        $this->SetFont('arialn','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        if (GetParam("m","")=="AUDIT") $this->Cell(200,5,"AS OF: BULAN - 13, ".GetParam("y",""),0,1,'C',0);
        else $this->Cell(200,5,"AS OF: ".$this->BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);

        $this->SetFont('arialn','',8);
        $this->Cell($this->ColSize[0],5,"AKTIVA",1,0,'C',0);
        $this->Cell($this->ColSize[1],5,"CURR. MONTH",1,0,'C',0);
        $this->Cell($this->ColSize[2],5,"LAST MONTH",1,0,'C',0);
        $this->Cell($this->ColSize[3],5,"PASIVA(KEWAJIBAN)",1,0,'C',0);
        $this->Cell($this->ColSize[4],5,"CURR. MONTH",1,0,'C',0);
        $this->Cell($this->ColSize[5],5,"LAST MONTH",1,0,'C',0);

        $this->Ln();
          $this->SetFont('arialn','',7);
  }

  function DBConnection ($Conn) {
    $this->DBConnection = $Conn;
  }

  function GenerateReport() {
    $SQL = "SELECT * FROM rpt_balance_sheet order BY placeorder";
    $MyResults = $this->DBConnection->get_results($SQL,ARRAY_A);
    foreach ($MyResults as $RowResults){
      $this->PutRowInArray($RowResults["deskripsi"],$RowResults["skada"],$RowResults["istotal"],$RowResults["layout"]);
    }
    $this->PutInLine();
  }

  function PutInLine() {
  	$this->ColSize = array(55,23,23,55,23,23);
    $rowcount = (count($this->LeftTree)>count($this->RightTree))?count($this->LeftTree):count($this->RightTree);
    //print_r($this->LeftTree);
    for ($x=0;$x<$rowcount;$x++) {
      if ($this->LeftTree[$x]["istotal"]==1) $LeftLine="TB";
      elseif ($this->LeftTree[$x]["istotal"]==2) {
        $DescLine="TB";
        $LeftLine="TB";
      } else {
        $DescLine="";
        $LeftLine="";
      }
      if ($this->RightTree[$x]["istotal"]==1) $RightLine="TB";
      elseif ($this->RightTree[$x]["istotal"]==2) {
        $DescLine="TB";
        $RightLine="TB";
      } else $RightLine="";

      //if ($this->LeftTree[$x]["deskripsi"])
      $this->Cell($this->ColSize[0],4,$this->LeftTree[$x]["deskripsi"],$DescLine.'LR',0,'L',0);
      if (strlen($this->LeftTree[$x]["skada"])) {
        $total_current_row = ($this->LeftTree[$x]["total_current"]<0)?"(".number_format($this->LeftTree[$x]["total_current"]*-1).")":number_format($this->LeftTree[$x]["total_current"]);
        $total_last_row    = ($this->LeftTree[$x]["total_last"]<0)?"(".number_format($this->LeftTree[$x]["total_last"]*-1).")":number_format($this->LeftTree[$x]["total_last"]);
        $this->Cell($this->ColSize[1],4,$total_current_row,$LeftLine.'LR',0,'R',0);
        $this->Cell($this->ColSize[2],4,$total_last_row,$LeftLine.'LR',0,'R',0);
      } else {
        $this->Cell($this->ColSize[1],4,"",'LR',0,'R',0);
        $this->Cell($this->ColSize[2],4,"",'LR',0,'R',0);
      }
      $this->Cell($this->ColSize[3],4,$this->RightTree[$x]["deskripsi"],$DescLine.'LR',0,'L',0);
      if (strlen($this->RightTree[$x]["skada"])) {
        $this->RightTree[$x]["total_current"] = $this->RightTree[$x]["total_current"]*-1;
        $this->RightTree[$x]["total_last"] = $this->RightTree[$x]["total_last"]*-1;
        $total_current_row = ($this->RightTree[$x]["total_current"]<0)?"(".number_format($this->RightTree[$x]["total_current"]*-1).")":number_format($this->RightTree[$x]["total_current"]);
        $total_last_row    = ($this->RightTree[$x]["total_last"]<0)?"(".number_format($this->RightTree[$x]["total_last"]*-1).")":number_format($this->RightTree[$x]["total_last"]);
        $this->Cell($this->ColSize[4],4,$total_current_row,$RightLine.'LR',0,'R',0);
        $this->Cell($this->ColSize[5],4,$total_last_row,$RightLine.'LR',0,'R',0);
        
      } else {
        $this->Cell($this->ColSize[4],4,"",'LR',0,'R',0);
        $this->Cell($this->ColSize[5],4,"",'LR',0,'R',0);
      }
      $this->Ln();
    }
  }

  function PutRowInArray($description,$skada,$istotal,$layout) {
      $countL = count($this->LeftTree);
      $countR = count($this->RightTree);

      if (strlen($skada)) {
        $SkadaInRow = explode(",",$skada);
        for ($x=0;$x<count($SkadaInRow);$x++) {
          if (strlen($WhereSQL)) $WhereSQL .= " or skada like '{$SkadaInRow[$x]}%' ";
          else $WhereSQL   .= " skada like '{$SkadaInRow[$x]}%' ";
        }
        $SQL = "SELECT sum(".$this->_SMonth.") as current, sum(".$this->_LSMonth.") as last FROM tblcoa WHERE ($WhereSQL) AND ledger='".$this->_CYear."'";
        //echo"$SQL"; exit;
        $RowResult = $this->DBConnection->get_row($SQL);


        //print $SQL."<p>\n";
        if ($layout=="L") {
          $this->LeftTree[$countL]["total_current"] = $RowResult->current;
          $this->LeftTree[$countL]["total_last"] = $RowResult->last;
        } else {
          $this->RightTree[$countR]["total_current"] = $RowResult->current;
          $this->RightTree[$countR]["total_last"] = $RowResult->last;
        }
      }
      if ($layout=="L") {
        $this->LeftTree[$countL]["deskripsi"] = $description;
        $this->LeftTree[$countL]["skada"] = $skada;
        $this->LeftTree[$countL]["istotal"] = $istotal;
      } else {
        $this->RightTree[$countR]["deskripsi"] = $description;
        $this->RightTree[$countR]["skada"] = $skada;
        $this->RightTree[$countR]["istotal"] = $istotal;
      }
//      print $this->LeftTree[$countL]["deskripsi"]."<p>\n";
  }

  function Period($Year, $Month){
    //Rama, (01 September 2009), Start
    //Output to PDF for AUDIT Period
    if ($Month == "AUDIT") {$Month = "13";
    //$Year = $Year + 1;
    }
    //Rama, (01 September 2009), End
    if ($Month<10) $Month = "0".$Month;
    if (($Month-1)<10) {$LMonth="0".($Month-1);}
    else $LMonth=$Month-1;
    $this->_CYear  = $Year;
    $this->_CMonth = $Month;
    $this->_SMonth = "aku".$Month;
    $this->_LastMonth = "aku".$LMonth;
    if ($Month==1) $this->_LSMonth = "awal";
    else $this->_LSMonth = "aku".$LMonth;
  }

  function BuildPeriod ($year, $month) {
    $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
    //Rama, (03 September 2009), Start
    if (GetParam("m", "") == "AUDIT") $year = $year + 1 ;
    //Rama, (03 September 2009), End
    return strtoupper(date("d F Y", mktime(0, 0, 0, $month+1, 0, $year)));
  }
  

}

?>
